namespace Defiant.SqlServer.PowerShellScriptTask
{
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using Microsoft.SqlServer.Dts.Runtime;

    public partial class PowerShellEditor : Form
    {
        private TaskHost taskHost;

        private PowerShellScriptTask scriptTask;

        private bool isLoadingVariables;

        public PowerShellEditor(TaskHost taskHost)
        {
            InitializeComponent();

            this.taskHost = taskHost;
            this.scriptTask = (PowerShellScriptTask)taskHost.InnerObject;
            this.isLoadingVariables = false;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            UpdateTaskValues();
            this.DialogResult = DialogResult.OK;
        }

        private void UpdateTaskValues()
        {
            this.scriptTask.Script = textScript.Text;
        }

        private string FormatForMultiLine(string s)
        {
            return s.Replace("\r", "").Replace("\n", "\r\n");
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void PowerShellEditor_Load(object sender, EventArgs e)
        {
            this.LoadVariables(checkBoxUserVariablesOnly.Checked);
            textScript.Text = FormatForMultiLine(this.scriptTask.Script);
        }

        private void LoadVariables(bool userVariablesOnly)
        {
            isLoadingVariables = true;
            checkedListBoxVariables.Items.Clear();

            foreach (Variable variable in taskHost.Variables)
            {
                if (variable.Namespace == "User" || (variable.Namespace == "System" && userVariablesOnly == false))
                {
                    bool isChecked = variable.ReadOnly
                                                       ? this.scriptTask.ReadOnlyVariables.Contains(variable.QualifiedName)
                                                       : this.scriptTask.ReadWriteVariables.Contains(variable.QualifiedName);
 
                    checkedListBoxVariables.Items.Add(variable.QualifiedName,isChecked);
                }
            }
            isLoadingVariables = false;
        }

        private void checkedListBoxVariables_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            if (isLoadingVariables) return;

            string variableName = (string)checkedListBoxVariables.Items[e.Index];
            Variable variable = this.GetVariableByName(variableName);
            List<string> variableStorage = variable.ReadOnly
                                                       ? this.scriptTask.ReadOnlyVariables
                                                       : this.scriptTask.ReadWriteVariables;

            if (e.NewValue == CheckState.Checked)
            {
                if (variableStorage.Contains(variableName) == false)
                {
                    variableStorage.Add(variableName);
                }

            }
            if (e.NewValue == CheckState.Unchecked)
            {
                variableStorage.Remove(variableName);
            }
        }

        private void checkBoxUserVariablesOnly_CheckedChanged(object sender, EventArgs e)
        {
            this.LoadVariables(checkBoxUserVariablesOnly.Checked);
        }

        private Variable GetVariableByName(string variableName)
        {
            foreach (Variable variable in taskHost.Variables)
            {
                if (variable.QualifiedName == variableName)
                {
                    return variable;
                }
            }

            return null;
        }
    }
}